﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tour.BLL;
using Tour.DAL;
public partial class Admin_Modules_Room_RoomList : System.Web.UI.Page
{
    public int? RoomTypeId { get; set; }
    public bool? IsVisible { get; set; }
    public bool? IsHome { get; set; }
    public string Expression { get; set; }
    public string Collate { get; set; }
    public static int PageSize = 20;
    public int PageIndex { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            //LoadDataRoomTypeDDL();
            GetPropertyFromQueryString();
            LoadData();
            // Response.Write(GetLocaleResourceString("Module.Home.NameWebsite"));
        }
    }
    void GetPropertyFromQueryString()
    {
        RoomTypeId = CommonHelper.QueryStringInt("TourTypeId", null);
        IsVisible = CommonHelper.QueryStringBoolNullable("IsVisible");
        IsHome = CommonHelper.QueryStringBoolNullable("IsHome");
        Expression = CommonHelper.QueryString(Expression, "CreatedDate");
        Collate = CommonHelper.QueryString("Collate", "DESC");
        PageIndex = CommonHelper.QueryStringInt("PageIndex", 1);
        ddlRoomType.SelectedValue = RoomTypeId.ToString();
        ddlVisible.SelectedValue = CommonHelper.BoolToIntString(IsVisible);
        ddlExpression.SelectedValue = Expression;
        ddlCollate.SelectedValue = Collate;
    }
    void GetPropertyFromForm()
    {
        RoomTypeId = ddlRoomType.SelectedValue.ToInt();
        IsVisible = CommonHelper.StringToBoolNullable(ddlVisible.SelectedValue.ToString());
        IsHome = CommonHelper.StringToBoolNullable(ddlHome.SelectedValue.ToString());
        Expression = ddlExpression.SelectedValue;
        Collate = ddlCollate.SelectedValue;
    }

    void LoadData()
    {
        int TotalRow = 0;
        grv.DataSource = TblRoomManager.CreateInstant().SelectTblRoomBy(null, RoomTypeId, "", IsVisible,IsHome, PageSize, PageIndex, out TotalRow, Expression, Collate);
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "PageIndex";
        smartPager.RowCount = TotalRow;
        smartPager.UrlFormatString = string.Format("RoomList.aspx?RoomTypeId={0}&IsVisible={1}&IsHome={2}&Expression={3}&Collate={4}", RoomTypeId, CommonHelper.BoolToIntString(IsVisible), CommonHelper.BoolToIntString(IsHome)
            , Expression, Collate) + "&PageIndex={0}";
        smartPager.Visible = TotalRow > PageSize;
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetPropertyFromForm();
        string Url = string.Format("RoomList.aspx?RoomTypeId={0}&IsVisible={1}&IsHome={2}&Expression={3}&Collate={4}", RoomTypeId, CommonHelper.BoolToIntString(IsVisible), CommonHelper.BoolToIntString(IsHome)
            , Expression, Collate);
        Response.Redirect(Url);
    }
    protected void grv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var hdfID = (HiddenField)grv.Rows[e.RowIndex].FindControl("hdfID");
        var hdfRoomTypeId = (HiddenField)grv.Rows[e.RowIndex].FindControl("hdfRoomTypeId");
        try
        {
            UpdateOrderOffAndDelete(hdfID.Value, hdfRoomTypeId.Value, 10000);
            TblRoomManager.CreateInstant().Delete(hdfID.Value.ToInt());
            Response.Redirect(Request.RawUrl);
            Trace.Warn("Delete Success" + hdfID.Value);
        }
        catch (Exception ex)
        {
            Trace.Warn("Delete Error" + hdfID.Value + ex.Message);
        }
        GetPropertyFromForm();
        LoadData();
        //Response.Redirect(Request.RawUrl);
    }
    protected void btnUpdateOrder_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < grv.Rows.Count; i++)
        {
            var txtOrder = (TextBox)grv.Rows[i].FindControl("txtOrder");
            var hdfOrder = (HiddenField)grv.Rows[i].FindControl("hdfOrder");
            var hdfRoomTypeId = (HiddenField)grv.Rows[i].FindControl("hdfRoomTypeId");
            var hdfID = (HiddenField)grv.Rows[i].FindControl("hdfID");
            int OldIndex = int.Parse(hdfOrder.Value);
            int NewIndex = int.Parse(txtOrder.Text);
            if (OldIndex != NewIndex)
                UpdateOrderOffAndDelete(hdfID.Value, hdfRoomTypeId.Value, NewIndex);
        }
        Response.Redirect(Request.RawUrl);
    }
    private void UpdateOrderOffAndDelete(string strId, string strParentId, int NewIndex)
    {
        string Whereclause = " And RoomTypeId IS NULL ";
        if (!strParentId.Equals(""))
        {
            Whereclause = String.Format(" And RoomTypeId={0}", strParentId);
        }
        string Whereclause1 = String.Format(" Id={0}", strId);
        string Whereclause2 = String.Format(" And Id<>{0}", strId);
        Tools.SqlHelper.UpdateOrders("TblRoom", Whereclause, Whereclause1, Whereclause2, NewIndex);
    }
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {//
        GetPropertyFromForm();
        grv.PageIndex = e.NewPageIndex;
        LoadData();
    }
}